package net.ibizsys.eclipse.studio.xml;

import java.io.File;

import net.ibizsys.eclipse.studio.Activator;
import net.ibizsys.eclipse.studio.data.IBizRuntimeType;
import net.ibizsys.eclipse.studio.util.StringKit;

import org.w3c.dom.Node;

/**
 * 文件
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizFile extends IBizSearchableNode
{
	public static final String NODE_IBIZFILE = "IBIZFILE";
	/**
	 * 类型
	 */
	public static final String ATTR_TYPE = "TYPE";

	/**
	 * FOLDER
	 */
	public static final String ATTR_FOLDER = "FOLDER";

	/**
	 * PUB
	 */
	public static final String ATTR_PUB = "PUB";

	/**
	 * USR
	 */
	public static final String ATTR_USR = "USR";

	private String folder;

	// 文件类型
	private IBizFileType type;
	// 相对路径
	private String path;
	// PUB文件全路径
	private String pubFilePath;
	// 用户文件全路径
	private String usrFilePath;
	// 用户真实文件全路径
	private String usrRealFilePath;
	
	private String showName;

	private String pub;
	private String usr;

	@Override
	public void init(Node node, IBizProject project)
	{
		super.init(node, project);

		this.initAttribute(ATTR_NAME, pub);

		if (this.getType().value() > IBizFileType.SF.value())
		{
			this.path = project.getSystem().getPkgpath() + File.separator + getFolder();
			this.pubFilePath = String.format("%1$s/%2$s/src/main/java/%3$s/%4$s", Activator.getDefault().getRootPath(), project.getPubPRJName(), path, pub);
			this.usrRealFilePath = String.format("%1$s/%2$s/src/main/java/%3$s/USR%4$s", Activator.getDefault().getRootPath(), project.getUsrPRJName(), path, pub);
		} else
		{
			// 判断父文件是否ng模式,如果是ng而且是controller则只usr为空
			if (this.getType() == IBizFileType.PF_CONTROLLER && (project.getRuntimeType() == IBizRuntimeType.NG))
			{
				this.usr = "";
			}

			if (project.isMavenProject())
			{
				this.path = project.getCodeName().toLowerCase() + File.separator + getFolder();
				this.pubFilePath = String.format("%1$s/%2$s/src/main/webapp/%3$s/%4$s", Activator.getDefault().getRootPath(), project.getPubPRJName(), path, pub);

				if (!StringKit.IsBlank(usr))
				{
					this.usrFilePath = String.format("%1$s/%2$s/src/main/webapp/%3$s/%4$s", Activator.getDefault().getRootPath(), project.getPubPRJName(), path, usr);
					this.usrRealFilePath = String.format("%1$s/%2$s/src/main/webapp/%3$s/%4$s", Activator.getDefault().getRootPath(), project.getUsrPRJName(), path, usr);
				}
			} else
			{
				this.path = project.getCodeName() + File.separator + getFolder();
				this.pubFilePath = String.format("%1$s/%2$s/%3$s/%4$s", Activator.getDefault().getRootPath(), project.getPubPRJName(), getFolder(), pub);

				if (!StringKit.IsBlank(usr))
				{
					this.usrFilePath = String.format("%1$s/%2$s/%3$s/%4$s", Activator.getDefault().getRootPath(), project.getPubPRJName(), getFolder(), usr);
					this.usrRealFilePath = String.format("%1$s/%2$s/%3$s/%4$s", Activator.getDefault().getRootPath(), project.getUsrPRJName(), getFolder(), usr);
				}
			}
		}
	}

	@Override
	protected void initAttribute(String name, String value)
	{
		if (StringKit.IsEqual(ATTR_TYPE, name, true))
		{
			this.type = IBizFileType.convert(value);
			return;
		}
		if (StringKit.IsEqual(ATTR_PUB, name, true))
		{
			this.pub = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_USR, name, true))
		{
			this.usr = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_FOLDER, name, true))
		{
			this.folder = value;
			return;
		}
		super.initAttribute(name, value);
	}

	public IBizFileType getType()
	{
		return type;
	}

	public String getPub()
	{
		return pub;
	}

	public String getFolder()
	{
		return folder;
	}

	public String getPath()
	{
		return path;
	}

	public String getPubFilePath()
	{
		return pubFilePath;
	}

	public String getUsrFilePath()
	{
		return usrFilePath;
	}

	public String getUsrRealFilePath()
	{
		return usrRealFilePath;
	}

	@Override
	public String getText()
	{
		if (!StringKit.IsBlank(this.showName))
		{
			return this.showName;
		}
		return getType().text();
	}
}
